unit USFPackCollectionBrowser;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, ComCtrls, ToolWin, ImgList, Menus;

type
  TfmUSFColl = class(TForm)
    ToolBar1: TToolBar;
    StatusBar1: TStatusBar;
    ListView1: TListView;
    Splitter1: TSplitter;
    ImageList1: TImageList;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    ImageList2: TImageList;
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    Info1: TMenuItem;
    About1: TMenuItem;
    Exit1: TMenuItem;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ListView1DblClick(Sender: TObject);
    procedure Exit1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure RefreshList;
  end;

var
  fmUSFColl: TfmUSFColl;

implementation

{$R *.dfm}

uses uPackage, uUSFExtendedPackage, uPackageCollection, USFPackageForm;

var USColl: TUSFPackageCollection;

function AppDir(): string;
begin
  Result := IncludeTrailingpathDelimiter(ExtractFilepath(Application.ExeName));
end;

procedure TfmUSFColl.FormCreate(Sender: TObject);
begin
  USColl := TUSFPackageCollection.Create(TUSFExtendedPackage);
end;

procedure TfmUSFColl.FormDestroy(Sender: TObject);
begin
  USColl.Free;
end;

procedure TfmUSFColl.ToolButton6Click(Sender: TObject);
begin
  ShowMessage('     Universal Stream Package Format'#10+
#13#10+
' Allows to store binary data (files, etc.) and variables (see'#10+
' TUSFDataClass for supported types) in one file.'#10+
'------------------------------------------------------------------------'#10+
' Copyright: (c) 2010, TSX Software'#10+
' License: Freeware');
end;

procedure TfmUSFColl.ToolButton2Click(Sender: TObject);
var s: string;
begin
  if InputQuery('Open packages','Enter path to package root folder:', s) then begin
    if s = '' then s := AppDir;
    USColl.Clear;
    USColl.LoadPackages(s);
    RefreshList;
  end;
end;

procedure TfmUSFColl.RefreshList;
var i: integer;
begin
  ListView1.Clear;
  for i := 0 to USColl.Count-1 do
    with ListView1.Items.Add do begin
      Caption := USColl.PackageByIndex[i].PackageName;
      Data := USColl.PackageByIndex[i];
    end;
end;

procedure TfmUSFColl.ListView1DblClick(Sender: TObject);
var i: integer;
begin
  if not Assigned(ListView1.Selected) then exit;
  if not Assigned(ListView1.Selected.Data) then exit;

  for i := 0 to self.MDIChildCount-1 do
    if TfmUSFPackage(self.MDIChildren[i]).OpenedPk = TUSFExtendedPackage(self.ListView1.Selected.Data) then
      exit;

  with TfmUSFPackage.Create(self) do begin
    OpenPk(TUSFExtendedPackage(self.ListView1.Selected.Data));
    Show;
  end;
end;

procedure TfmUSFColl.Exit1Click(Sender: TObject);
begin
  Close;
end;

end.
